package com.lhj.bootdemo.controller;

import com.lhj.bootdemo.util.DownLoadFileUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * @author lhj
 * @version 1.0
 * @date 2021/8/20 4:23 下午
 */
@Slf4j
@RestController
public class TestController {

    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Value("${test.word}")
    private String testWord;

//    @Resource
//    private RedissonClient lhjRedissonClient;



    @GetMapping("/hello")
    public String hello() {
        System.out.println("开始执行hello方法");
        System.out.println("当前线程："+Thread.currentThread().getName());
        return "hello fool";
    }

    @RequestMapping("downloadEmployeeTemplate")
    @ApiOperation(value = "人员管理-模板下载", notes = "#{audit.custom}")
    public void downloadEmployeeTemplate(HttpServletRequest request, HttpServletResponse response) {
        String path = Thread.currentThread().getContextClassLoader().getResource("template").getPath();
        String needPath = "/calculate_template.xlsx";
        String resultPath = "/"+path+needPath;
        try {
            DownLoadFileUtil.downloadFile(request, response, resultPath, "abc.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response) {
        //这里文件名称是通过参数传递过来的，要是不需要，直接可以写在这里。String fileName = "a.doc";
        String path1 = Thread.currentThread().getContextClassLoader().getResource("template").getPath();
        String needPath = "/calculate_template.xlsx";
        String path = "/"+path1+needPath;
        String fileName = "abc.xlsx";


        fileDownLoad(response, path, fileName);
    }

    private void fileDownLoad(HttpServletResponse response, String path, String fileName) {
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            File file = new File(path);
            response.setContentType("application/x-download");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("The file not found!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * swagger 测试
     * @param map
     * @return
     */
    @ApiOperation(value = "swagger测试")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = String.class)})
    @PostMapping("/testSwagger")
    public String index(@RequestBody Map map) {
        System.out.println("swagger测试");
        String result = testWord;
        return result;
    }

//    @ApiOperation(value = "swagger测试")
//    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = String.class)})
//    @PostMapping("/testRedis")
//    public Object testRedis() {
//
//        RSet<Object> helloRedis = lhjRedissonClient.getSet("hello redis");
//        RKeys keys = lhjRedissonClient.getKeys();
//        System.out.println(keys.toString());
//        return keys;
//    }
}
